﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Mindfor.Web.Models;

namespace Mindfor.Web.Modules.Search.Controllers
{
	public class SearchController : CmsController
	{
		[HttpGet]
		public ActionResult Index(string text)
		{
			SearchModel model = new SearchModel(text);
			model.Results = GetSearchResults(text);
			return View(model);
		}

		[HttpPost]
		public ActionResult Index(SearchModel model)
		{
			return RedirectToAction("Index", model);
		}

		[Widget("Search")]
		public ActionResult Widget()
		{
			return PartialView();
		}

		private List<SearchResult> GetSearchResults(string text)
		{
			List<SearchResult> results = new List<SearchResult>();
			if (!String.IsNullOrEmpty(text))
			{
				foreach (ModuleBase module in ModuleFactory.Default.Modules.GetEnabledModules())
					results.AddRange(module.Search(Data, Url, text));
			}

			return results;
		}
	}
}
